import pyttsx3
from pydub import AudioSegment
import os
# 添加FFmpeg路径到环境变量
# ffmpeg_path = r"C:\Program Files\ImageMagick-7.1.1-Q16-HDRI\ffmpeg.exe"
# os.environ["PATH"] += os.pathsep + os.path.dirname(ffmpeg_path)
ffmpeg_path = r"C:\Program Files\ImageMagick-7.1.1-Q16-HDRI\ffmpeg.exe"
AudioSegment.ffmpeg = ffmpeg_path
AudioSegment.ffprobe = ffmpeg_path

def speak_text(text, output_path):
    engine = pyttsx3.init()
    engine.setProperty('rate', 150)  # 设置语速（默认为200）
    engine.setProperty('volume', 1)  # 设置音量（范围为0.0到1.0）
    engine.setProperty('voice', 'zh')  # 设置语音（默认为系统默认语音）
    engine.save_to_file(text, output_path)
    engine.runAndWait()


# os.putenv('PATH', r'C:\Program Files\ImageMagick-7.1.1-Q16-HDRI')
# AudioSegment.converter  = r"C:\Program Files\ImageMagick-7.1.1-Q16-HDRI\ffmpeg.exe"
# AudioSegment.avconv = "path/to/avconv"
with open("./folder/output.txt", 'r') as file:
    text = file.read()
output_path = "./folder/output.mp3"

speak_text(text, output_path)
